const app = getApp();

Page({
  data: {
    noCart:'yes',
    cartList: '',
    cartCheck:{},
    allCheck : true,
    count_price:0,
    price:0,
    count_number:0,
    number:0,
    aid:0
  },
  onShareAppMessage() {
    return {
      title: '花礼鲜花玫瑰生日',
      desc: '花礼鲜花玫瑰生日',
      path: 'pages/todos/todos'
    };
  },
  onLoad(){
    my.setNavigationBar({
      title: '我的购物车',
    });
    var that = this;
    my.removeStorage({
      key: 'address'
    });
    my.getStorage({
      key: 'token',
      success: function(res) {        
        my.httpRequest({
          url: app.geturl+'/member-address/get-default',
          method: 'GET',
          data:{
            token:res.data.token
          },
          dataType: 'json',
          success: function(re) {
            if(re.data.code==0){
              if(re.data.data!=null){
                that.setData({
                  aid:re.data.data.member_address_id
                });
              }
            }else{
              app.memberLogin();
            }
          }
        });
      }
    });
    this.cartList();
  },
  onShow(e) {
    this.cartList();
  },
  cartList(){
    var that = this;
    my.getStorage({
      key: 'token',
      success: function(res) {  
        my.httpRequest({
          url: app.geturl+'/cart/list',
          method: 'GET',
          data: {
            token:res.data.token
          },
          dataType: 'json',
          success: function(res) {
            if(res.data.code==0){
              if(res.data.data.list){
                console.info(res.data.data.list);
                if(typeof res.data.data.list[1] !="undefined"){
                  var num = res.data.data.list[1].goods_list.length;
                  var c_price = 0;
                  var arr = new Object();
                  for(var i=0;i<num;i++){
                    c_price = that.accSub(res.data.data.list[1].goods_list[i].price*res.data.data.list[1].goods_list[i].buy_num,c_price,2);
                    var oneArr = new Object();
                    oneArr['check'] = true;
                    oneArr['price'] = res.data.data.list[1].goods_list[i].price;
                    oneArr['num'] = res.data.data.list[1].goods_list[i].buy_num;
                    oneArr['goods_id'] = res.data.data.list[1].goods_list[i].goods_id;
                    oneArr['cart_id'] = res.data.data.list[1].goods_list[i].cart_id;
                    arr[i] = oneArr;
                  }
                  that.setData({
                    cartList:res.data.data.list[1].goods_list,
                    count_number:num,
                    number:num,
                    count_price:c_price,
                    price:c_price,
                    cartCheck:arr,
                  });
                }else{
                  that.setData({
                    noCart:'no'
                  });
                }
              }
            }else{
              app.memberLogin();
            }
          }
        }); 
      }
    });
  },
  accSub(arg1,arg2,type){
    var r1,r2,m,n;
    try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
    try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
    m=Math.pow(10,Math.max(r1,r2));
    //last modify by deeka
    //动态控制精度长度
    n=(r1>=r2)?r1:r2;
    if(type==1){
        return ((arg1*m-arg2*m)/m).toFixed(2);
    }else{
        return ((arg1*m+arg2*m)/m).toFixed(2);
    }
  },
  oneCheck(e){
    var that = this;
    var num = parseInt(e.target.dataset.num);
    var data = that.data.cartCheck;
    if(data[num].check){
      data[num].check = false;
    }else{
      data[num].check = true;
    }
    that.setData({
      cartCheck:data
    });
    that.countPrice();
  },
  upCart(e){
    var that = this;
    var num = parseInt(e.target.dataset.num);
    var type = e.target.dataset.type;
    var id = e.target.dataset.id;
    if(type==1){
      num = num+1;
    }else{
      num = num-1;
    }
    my.getStorage({
      key: 'token',
      success: function(res) {  
        my.httpRequest({
          url: app.geturl+'/cart/update',
          method: 'GET',
          data: {
            token:res.data.token,
            goods_list:id+':'+num
          },
          dataType: 'json',
          success: function(res) {
            that.cartList();
            console.info(res);
          }
        }); 
      }
    });
  },
  allCheck(e){
    var that = this;
    var aCheck = that.data.allCheck;
    var num = that.data.count_number;
    if(aCheck){
      var sc = false;
      var number = 0;
    }else{
      var sc = true;
      var number = num;
    }
    var cart = that.data.cartCheck;
    for(var i=0;i<num;i++){
      if(cart[i].check){
        cart[i].check = sc;
      }
    }
    console.info(cart);
    that.setData({
      cartCheck:cart,
      allCheck:sc
    });
    that.countPrice();
  },
  countPrice(){
    var that = this;
    var check = that.data.cartCheck;
    var num = that.data.count_number;
    var c_price = 0;
    var number = 0;
    for(var i=0;i<num;i++){
      if(check[i].check){
        c_price = that.accSub(check[i].price*check[i].num,c_price,2);
        number++;
      }
    }
    that.setData({
      price:c_price,
      number:number
    });
    console.info(c_price);
  },
  clearCart(){
    var that = this;
    var check = that.data.cartCheck;
    var num = that.data.count_number;
    var goods_id = '';
    for(var i=0;i<num;i++){
      if(check[i].check){
        goods_id+=check[i].goods_id+',';
      }
    }
    var goods_id = goods_id.substr(0,goods_id.length-1);
    console.info(goods_id);
    my.confirm({
      title: '温馨提示',
      content: '您确定要删除吗？',
      confirmButtonText: '删除',
      cancelButtonText: '取消',
      success: (result) => {
        if(result.confirm){
          my.getStorage({
            key: 'token',
            success: function(res) {  
                my.httpRequest({
                  url: app.geturl+'/cart/remove',
                  method: 'GET',
                  data: {
                    token:res.data.token,
                    goods_ids:goods_id
                  },
                  dataType: 'json',
                  success: function(res) {
                    if(res.data.code==0){
                      that.cartList();
                      console.info(res);
                    }else{
                      app.memberLogin();
                    }
                  }
                }); 
            }
          });
        }
      },
    });
  },
  subCart(){
    var that = this;
    var check = that.data.cartCheck;
    var num = that.data.count_number;
    var goods_id = '';
    var cart_id = '';
    for(var i=0;i<num;i++){
      if(check[i].check){
        goods_id+=check[i].goods_id+':'+check[i].num+',';
        cart_id+=check[i].cart_id+',';
      }
    }
    var goods_id = goods_id.substr(0,goods_id.length-1);
    var cart_id = cart_id.substr(0,cart_id.length-1);
    console.info(cart_id);
    if(!cart_id){
      my.alert({content: '请至少选择一件商品'});
      return;
    }
    my.navigateTo({ url: '../add-todo/comfirn?is_cart=1&goods_id='+goods_id+'&cart_id='+cart_id+'&aid='+that.data.aid });
  },
  add() {
    app.todos = app.todos.concat([{
      text: this.data.inputValue,
      compeleted: false,
    }]);

    my.navigateBack();
  },
  handleTap(){
    my.navigateTo({ url: '../todos/todos' });
  }
});
